﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;

public partial class BasicInfo_SupplierDetail : Frame.Pages.DetailEditPage
{
    DAL.css_csdj m_model;
    protected override object Model
    {
        get
        {
            if (m_model == null)
            {
                if (ModelAction == Frame.Pages.ActionType.New)
                {
                    m_model = new DAL.css_csdj();

                    m_model.edate = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                    m_model.editor = "0001";// CMSUser.Current.Name;
                    m_model.fdate = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                    m_model.founder = "0001"; //CMSUser.Current.Name;

                    return m_model;
                }

                if (!string.IsNullOrEmpty(KeyID))
                {
                    m_model = this.DataSession.GetTable<DAL.css_csdj>().FirstOrDefault(o => o.csbm == KeyID);

                    m_model.edate = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                    m_model.editor = "0001";// CMSUser.Current.Name;

                    return m_model;
                }
                else
                {
                    return null;
                }

            }
            else
            {

                m_model.edate = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                m_model.editor = "0001";// CMSUser.Current.Name;

                return m_model;
            }

        }
    }

    protected override string ScriptBackUrl
    {
        get { return "SupplierEdit.aspx"; }
    }

    protected override void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ModelAction == Frame.Pages.ActionType.Update)
            {
                Edit_csbm.ReadOnly = true;
            }
        }
    }

    protected override void SubmitBtn_Click(object sender, EventArgs e)
    {

        UpdateUser();
        base.SubmitBtn_Click(sender, e);

        #region 父类默认实现
        //try
        //{
        //    string msg;
        //    if (ModelAction == Frame.Pages.ActionType.New)
        //    {
        //        DataSession.GetTable(Model.GetType()).InsertOnSubmit(Model);
        //        msg = "新增成功！";
        //    }
        //    else
        //    {
        //        msg = "修改成功！";
        //    }
        //    DataSession.SubmitChanges();
        //    Frame.Global.GlobalFunctions.AlertToUrl(msg, ScriptBackUrl);
        //}
        //catch (Exception ex)
        //{
        //    Frame.Global.GlobalFunctions.AlertToUrl(string.Format("操作发生错误{0}，请稍后重试！", ex.Message), ScriptBackUrl);
        //}
        #endregion
    }

    void initUser()
    {
        //string users = "";
        using (DAL.DataClassesDataContext db = new DAL.DataClassesDataContext())
        {
            var rybms = db.css_csdj_rybm.Where(r => r.csbm == m_model.csbm);
            foreach (var item in rybms)
            {
                DAL.css_user user = db.css_user.FirstOrDefault(r => r.rybm == item.rybm);
                if (user != null)
                {

                }
            }
        }
        Edit_user.Text = "";

    }

    /// <summary>
    /// 保存厂商人员关联
    /// </summary>
    void UpdateUser()
    {
        using (DAL.DataClassesDataContext db = new DAL.DataClassesDataContext())
        {
            ASPxListBox lb = Edit_user.FindControl("listBox") as ASPxListBox;
            foreach (var item in lb.SelectedValues)
            {
                DAL.css_csdj_rybm cr = new DAL.css_csdj_rybm();
                cr.csbm = Edit_csbm.Text;
                cr.rybm = item.ToString();
                db.css_csdj_rybm.InsertOnSubmit(cr);
            }
            db.SubmitChanges();
        }
    }

    protected void CancelBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/BasicInfo/SupplierEdit.aspx");
    }

    protected override string ActionID
    {
        get { return "0"; }
    }

}